package com.liao.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liao.ticket.dao.mapper.PlaceMapper;
import com.liao.ticket.dao.pojo.Place;
import com.liao.ticket.service.PlaceService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.executor.statement.StatementUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PlaceServiceImpl implements PlaceService {
    @Autowired
    private PlaceMapper placeMapper;

    @Override
    public List<Place> allProvince() {
        return selectByTypeAndPid(1,1);
    }

    @Override
    public Place oneProvince(Integer id) {
        return selectByTypeAndId(1,id);
    }

    @Override
    public List<Place> listCity(Integer id) {
        return selectByTypeAndPid(2, id);
    }

    @Override
    public Place oneCity(Integer id) {
        return selectByTypeAndId(2,id);
    }

    @Override
    public List<Place> listCounty(Integer id) {
        return selectByTypeAndPid(3, id);
    }

    @Override
    public Place oneCounty(Integer id) {
        return selectByTypeAndId(3,id);
    }

    @Override
    public List<Place> listPlace(String name) {
        if(StringUtils.isBlank(name)){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<Place> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(Place::getName, name);
        return placeMapper.selectList(wrapper);
    }

    private List<Place> selectByTypeAndPid(Integer type, Integer pid) {
        if(type == null || pid == null){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<Place> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Place::getType, type).eq(Place::getPid, pid);
        return placeMapper.selectList(wrapper);
    }

    private Place selectByTypeAndId(Integer type, Integer id){
        if (type == null || id == null) {
            return new Place();
        }
        LambdaQueryWrapper<Place> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Place::getType, type).eq(Place::getId, id);
        return placeMapper.selectOne(wrapper);
    }
}
